options( java.parameters = "-Xmx16g" )
options(stringsAsFactors = F)
library(rJava)
classpath_dir<-'Java'
jars<-c('gt-epsg-hsql-14.5.jar','gt-process-feature-14.5.jar',
        'jai_imageio-1.1.jar','tutorial-0.0.1-SNAPSHOT-jar-with-dependencies.jar')
library(dplyr)
class_path<-sapply(jars,function(x){file.path(classpath_dir,x)})
.jinit('Java/bin',force.init = T)
.jaddClassPath(class_path[1])
.jaddClassPath(class_path[2])
.jaddClassPath(class_path[3])
.jaddClassPath(class_path[4])
.jaddClassPath('Java/bin/')
.jclassPath()
.jcall("java/lang/System", method = "gc")
obj=.jnew("testit/Testit")
jheat<-function(input_path=NULL,field_name='value',radius=10,output_path=NULL,width=100,height=100,manualASP=F)
{
    # java Testit <input_path> <field_name> <radius> <output_path> <width> <height>
    .jcall(obj,"V","main",
           .jarray(c(input_path,field_name,radius,output_path,width,height,manualASP), "java/lang/String")) 
}
# devtools::install_git('https://git.oschina.net/seifer_earth/heat_creprice.git')
library(heat.creprice)
qd<-read.heatmap(city_code = 'qd')
library(rgdal)
proj4string(qd)<-'+init=epsg:4326'
file.remove('heatdata.shp')
writeOGR(qd,'heatdata.shp',layer = 'heatdata',driver = 'ESRI Shapefile',overwrite_layer = T)

file.remove('test.tif')
jheat(input_path ='heatdata.shp' 
      ,radius =6,field_name ='value' ,
      output_path = 'test.tif',width = 1500,height = 1500)



library(raster)
library(leaflet)
r <- raster("test.tif")
r[r<=0.02]<-NA
pal <- colorNumeric(c("green", "yellow", "orange","red","red","red"), values(r),alpha = T,
                    na.color = "transparent")

# pal.bin <- colorBin(c("green", "yellow", "red"), values(r),bins = c(0.1,0.5,1),
#                     na.color = "transparent")
leaflet() %>% addTiles() %>%
    addRasterImage(r, colors = pal, opacity = 0.75) %>%
    addLegend(pal = pal, values = values(r),
              title = "Surface of listing count")
